﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RapChieuPhim.DTO;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using RapChieuPhim.BUS;

namespace RapChieuPhim.DAO
{
    class CTRapDAO
    {
        public CTRapDTO GetByID(int rap, int phim)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@MaRap", rap);
            param[1] = new SqlParameter("@MaPhim", phim);
            DataTable dt = cn.SqlExcuteQuery("LayCTRapTheoID", param);
            CTRapDTO CTRap = new CTRapDTO();

            if (dt.Rows.Count != 0)
            {
                CTRap.Rap = RapBUS.GetByID(int.Parse(dt.Rows[0]["MaRap"].ToString()));
                CTRap.Phim = PhimBUS.GetByID(int.Parse(dt.Rows[0]["MaPhim"].ToString()));
            }
            return CTRap;
        }

        public IList GetAll()
        {
            DataProvider cn = new DataProvider();
            ArrayList array = new ArrayList();
            DataTable dt = cn.SqlExcuteQuery("LayAllCTRap");

            if (dt.Rows.Count != 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    CTRapDTO CTRap = new CTRapDTO();
                    CTRap.Rap = RapBUS.GetByID(int.Parse(dr["MaRap"].ToString()));
                    CTRap.Phim = PhimBUS.GetByID(int.Parse(dr["MaPhim"].ToString()));
                    array.Add(CTRap);
                }
            }
            return array;
        }

        public void Insert(CTRapDTO CTRap)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@MaRap", CTRap.Rap);
            param[1] = new SqlParameter("@MaPhim", CTRap.Phim);
            cn.SqlExcuteNonQuery("InsertCTRap", param);
        }

        public void Update(CTRapDTO CTRap)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@MaRap", CTRap.Rap);
            param[1] = new SqlParameter("@MaPhim", CTRap.Phim);
            cn.SqlExcuteNonQuery("UpdateCTRap", param);
        }

        public void Delete(int rap, int phim)
        {
            DataProvider cn = new DataProvider();
            SqlParameter[] param = new SqlParameter[2];
            param[0] = new SqlParameter("@MaRap", rap);
            param[1] = new SqlParameter("@MaPhim", phim);
            cn.SqlExcuteNonQuery("DeleteCTRap", param);
        }
    }
}
